/*
 * PlaylistDownAction.java
 *
 * Created on April 2, 2006, 1:13 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.lib.actions;

import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.bjj.utils.Resources;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ImageIcon;
import org.bff.slimserver.SlimFavoritePlugin;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.exception.SlimException;
import org.bff.slimserver.musicobjects.SlimObject;
import org.bff.slimserver.musicobjects.SlimPlayableObject;

/**
 *
 * @author Bill
 */
public class AddFavoriteAction extends AbstractAction {

    private SlimFavoritePlugin favoritePlugin;
    private SlimPlayableObject favorite;
    private String year;
    private static ImageIcon iconActive;
    private static ImageIcon iconDisabled;

    /**
     * Creates a new instance of PlaylistDownAction
     * 
     * until the year conversion is done
     */
    public AddFavoriteAction(String year) {
        this(year, false);
    }

    public AddFavoriteAction(String year, boolean abbreviated) {
        this.year = year;
        try {
            this.favoritePlugin = JukeboxProperties.getInstance().getFavoritesPlugin();
        } catch (SlimConnectionException ex) {
            Logger.getLogger(AddFavoriteAction.class.getName()).log(Level.SEVERE, null, ex);
        }

        loadDetails(abbreviated);
    }

    public AddFavoriteAction(SlimPlayableObject favorite) {
        this(favorite, false);
    }

    public AddFavoriteAction(SlimPlayableObject favorite, boolean abbreviated) {
        try {
            this.favorite = favorite;
            this.favoritePlugin = JukeboxProperties.getInstance().getFavoritesPlugin();
        } catch (SlimConnectionException ex) {
            Logger.getLogger(AddFavoriteAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        loadDetails(abbreviated);
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        try {
            if (getFavorite() != null) {
                getFavoritePlugin().addFavorite(getFavorite());
            } else if (getYear() != null) {
                getFavoritePlugin().addFavorite(getYear(), SlimObject.URL_PREFIX_YEAR + getYear());
            }
        } catch (SlimException ex) {
            Logger.getLogger(AddFavoriteAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * @return the favorite
     */
    public SlimPlayableObject getFavorite() {
        return favorite;
    }

    /**
     * @param favorite the favorite to set
     */
    public void setFavorite(SlimPlayableObject favorite) {
        this.favorite = favorite;
    }

    /**
     * @return the favoritePlugin
     */
    public SlimFavoritePlugin getFavoritePlugin() {
        return favoritePlugin;
    }

    /**
     * @param favoritePlugin the favoritePlugin to set
     */
    public void setFavoritePlugin(SlimFavoritePlugin favoritePlugin) {
        this.favoritePlugin = favoritePlugin;
    }

    /**
     * @return the year
     */
    public String getYear() {
        return year;
    }

    /**
     * @param year the year to set
     */
    public void setYear(String year) {
        this.year = year;
    }

    public void setExistsIcon() {
        putValue(Action.SMALL_ICON, iconDisabled);
    }

    public void setNormalIcon() {
        putValue(Action.SMALL_ICON, iconActive);
    }

    @Override
    public void setEnabled(boolean enabled) {
        super.setEnabled(enabled);

        if (enabled) {
            setNormalIcon();
        } else {
            setExistsIcon();
        }
    }

    private void loadDetails(boolean abbreviated) {
        if (iconActive == null) {
            iconActive = Resources.getIcon(Resources.ICON_KEY.ACTION_FAVORITES_ENABLED);
        }

        if (iconDisabled == null) {
            iconDisabled = Resources.getIcon(Resources.ICON_KEY.ACTION_FAVORITES_DISABLED);
        }

        if (abbreviated) {
            putValue(Action.NAME, Resources.getMessage(Resources.LABEL_KEY.ACTION_FAVORITE));
        } else {
            putValue(Action.NAME, Resources.getMessage(Resources.LABEL_KEY.ACTION_FAV_ADD_NAME));
        }
        putValue(Action.SMALL_ICON, iconActive);
        putValue(Action.SHORT_DESCRIPTION, Resources.getMessage(Resources.LABEL_KEY.ACTION_FAV_ADD_SHORT_DESC));
    }
}
